package secondBrush.list;
//指针方法
public class E04Leetcode19_3 {
    public ListNode removeNthFromEnd(ListNode head, int n) {

        ListNode dummyHead = new ListNode(-1, head);
        ListNode slowHead = dummyHead;
        ListNode fastHead = dummyHead;

        for (int i = 0; i < n + 1; i++) {
            fastHead = fastHead.next;
        }

        while (fastHead != null) {
            fastHead = fastHead.next;
            slowHead = slowHead.next;
        }

        slowHead.next = slowHead.next.next;

        return dummyHead.next;



    }


    public class ListNode {
        int val;
        ListNode next;

        ListNode() {
        }

        ListNode(int val) {
            this.val = val;
        }

        ListNode(int val, ListNode next) {
            this.val = val;
            this.next = next;
        }
    }
}
